#include <iostream>
#include <zmq.h>
#include "test_zero_mq.pb.h"

int main() {
    void* context = zmq_ctx_new();
    void* socket = zmq_socket(context, ZMQ_REP);
    zmq_bind(socket, "tcp://*:5555");

    while (true) {
        zmq_msg_t zmq_request;
        zmq_msg_init(&zmq_request);
        zmq_msg_recv(&zmq_request, socket, 0);

        messages::Request request;
        request.ParseFromArray(zmq_msg_data(&zmq_request), zmq_msg_size(&zmq_request));

        std::cout << "Received request: " << request.data() << std::endl;

        messages::Response response;
        response.set_result("Hello, " + request.data());

        std::string serialized_response;
        response.SerializeToString(&serialized_response);

        zmq_msg_t zmq_response;
        zmq_msg_init_size(&zmq_response, serialized_response.size());
        memcpy(zmq_msg_data(&zmq_response), serialized_response.data(), serialized_response.size());
        zmq_msg_send(&zmq_response, socket, 0);
    }

    zmq_close(socket);
    zmq_ctx_destroy(context);

    return 0;
}
